package com.epam.weblibrary.repositories;

import com.epam.weblibrary.entities.book.Book;
import com.epam.weblibrary.entities.user.Roles;
import com.epam.weblibrary.entities.user.User;
import com.epam.weblibrary.repositories.book.BookRepository;
import com.epam.weblibrary.repositories.user.UserRepository;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.InputStream;

public class DbInit {

    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private UserRepository userRepository;

    public void initialize(){

        //create users
        User simpleUser = new User("user", "123", "Iurii", "Miedviediev", "druidkuma@gmail.com", Roles.ROLE_USER);
        simpleUser.setDiscount(0.3F);

        User adminUser = new User("admin", "admin", "Admin", "Adminov", "iurii_miedviediev@epam.com", Roles.ROLE_ADMIN);
        adminUser.setDiscount(0.5F);

        userRepository.save(simpleUser);
        userRepository.save(adminUser);

        //create books
        String[] titles = new String[] {"Harry Potter and the Philosopher's Stone",
                "Harry Potter and the Chamber of Secrets",
                "Harry Potter and the Prisoner of Azkaban",
                "Harry Potter and the Goblet of Fire",
                "Harry Potter and the Order of the Phoenix",
                "Harry Potter and the Half-Blood Prince",
                "Harry Potter and the Deathly Hallows"};

        String author = "J. K. Rowling";
        Integer[] years = new Integer[] {1997, 1998, 1999, 2000, 2003, 2005, 2007};
        String[] descriptions = new String[] {
                "The plot follows Harry Potter, " +
                        "a young wizard who discovers his magical heritage, as he makes close " +
                        "and a few enemies in his first year at the Hogwarts School of Witchcraft and Wizardry. " +
                        "With the help of his friends, Harry faces an attempted comeback by the dark wizard " +
                        "Lord Voldemort, who killed Harry's parents, but failed to kill Harry when he was just " +
                        "one year old.",
                "The plot follows Harry's second year at Hogwarts School of Witchcraft and Wizardry, " +
                        "during which a series of messages on the walls of the school's corridors warn " +
                        "that the 'Chamber of Secrets' has been opened and that the 'heir of Slytherin' " +
                        "would kill all pupils who do not come from all-magical families. These threats are " +
                        "followed by attacks which leave residents of the school 'petrified' (frozen like " +
                        "stone). Throughout the year, Harry and his friends Ron Weasley and Hermione Granger " +
                        "investigate the attacks.",
                "The book follows Harry Potter, a young wizard, in his third year at Hogwarts School of Witchcraft " +
                        "and Wizardry. Along with friends Ron Weasley and Hermione Granger, Harry investigates " +
                        "Sirius Black, an escaped prisoner from Azkaban whom they believe is one of Lord Voldemort's " +
                        "old allies.",
                "Harry Potter and the Goblet of Fire is the fourth novel in the Harry Potter series, " +
                        "written by British author J. K. Rowling. It follows Harry Potter, a wizard in his " +
                        "fourth year at Hogwarts School of Witchcraft and Wizardry and the mystery " +
                        "surrounding the entry of Harry's name into the Triwizard Tournament, in which he " +
                        "is forced to compete.",
                "It follows Harry Potter's struggles through his fifth year at Hogwarts School of Witchcraft " +
                        "and Wizardry, including the surreptitious return of the antagonist Lord Voldemort, " +
                        "O.W.L. exams, and an obstructive Ministry of Magic.",
                "Set during protagonist Harry Potter's sixth year at Hogwarts, the novel explores the past of " +
                        "Harry's nemesis, Lord Voldemort, and Harry's preparations for the final battle alongside " +
                        "his headmaster and mentor Albus Dumbledore.",
                "Major themes in the novel are death and living in a corrupted society, and critics have " +
                        "compared them to Christian allegories. Generally well-received, the book won the " +
                        "2008 Colorado Blue Spruce Book Award, and the American Library Association named it " +
                        "a \"Best Book for Young Adults\". A two-part film adaptation began showing in " +
                        "November 2010 when Harry Potter and the Deathly Hallows – Part 1 was released; " +
                        "Part 2 was released on 15 July 2011."};

        for(int j = 0; j < 2; j++) {
            for(int i = 0; i < 7; i++) {
                byte[] bFile = null;

                try {
                    InputStream image = getClass().getResourceAsStream("/book-covers/hp_" + (i+1) + ".jpg");
                    bFile = IOUtils.toByteArray(image);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Book book = new Book(titles[i], author, years[i], descriptions[i], 99L);
                book.setCover(bFile);

                bookRepository.save(book);
            }
        }

    }
}